#include "nstar_adt_httputil.h"
#include "nstar_adt_http_page.h"
#include "oxyct.h"

#define JS_P1_E1 		"sip"
#define JS_P1_E2 		"dip"
#define JS_P1_E3 		"programIP"
#define JS_P1_E4 	    "logicID"
#define JS_P1_E5 	    "physID"
#define JS_P1_E6 	    "physID2"
#define JS_P1_E7 	    "fmsta"

#define C_PAGE_NAME HTML_PAGE1_NAME

#define visit_passback_net_sta() ""
#define visit_pg_sourceid() ""
#define visit_pg_url() ""


#define C_PAGE_BODY_0 "<body>"\
"<ul>"\
"<li><a class='home'>康拓-KTYX</a></li>"\
"<li><a href='"HTML_PAGE1_NAME".html' class='active'>模拟状态</a></li>"\
"<li><a href='"HTML_PAGE2_NAME".html'>模拟输入</a></li>"\
"<li><a href='"HTML_PAGE3_NAME".html'>故障记录</a></li>"\
"<li><a href='"HTML_PAGE4_NAME".html'>OEM信息</a></li>"\
"</ul>"\
"<h2> chengdu kangte web management interface </h2>"\
"<h3> 模拟状态</h3>"\
"<div style='background:snow; display:block; padding:10px 10%;'>"\
"<form id='frmSetting' method='POST' action='"C_PAGE_NAME".cgi'>"\


#define C_PAGE_BODY_2 "</form>"\
"</div>"\
"</body>"\
"</html>"\


#define STA_REFRESH_HTML_START "<script>function $(id) { return document.getElementById(id); };"\
"function myrefresh()"\
"{"\
"	var xmlHttp = new XMLHttpRequest();"\
"	xmlHttp.open('GET', '/sta.js', true);"\
"	xmlHttp.send(null);"\
"	xmlHttp.onreadystatechange=function()"\
"	{"\
"		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) "\
"		{"\
		"var json=xmlHttp.responseText;"\
		"var o=eval(\'(\'+json+\')\');"\

#define STA_REFRESH_HTML_END  "}"\
"	}"\
"}</script>	"\
"<script language='JavaScript'>setInterval('myrefresh()',2000);</script>"\
"<script>myrefresh();</script>"

static void _htm_work_mode(char *content, uint32_t len) {
    snprintf(content, len, "%s", osta.work_mode == MODE_WORK ? "工作" : "待机");
}
static void _htm_work_sta(char *content, uint32_t len) {
    snprintf(content, len, "%s", ctl_is_power_on() ? "制氧中" : "停止");
}
static uint8_t unuse = 0;

static struct js_node list[] = {
    {"工作模式", 26, 0, _htm_work_mode, NULL, &osta.work_mode, 1, NULL},
    {"制氧状态", 26, 0, _htm_work_sta, NULL, &unuse, 1, NULL},    
    {"氧分实际值", 26, 0, NULL, NULL, &osta.o2_pres, 2, NULL},
    {"氧分压预期值", 26, 0, NULL, NULL, &osta.o2_pres_pend, 2, NULL},
    {"氧分压显示值", 26, 0, NULL, NULL, &osta.o2_pres_display, 2, NULL}, 
    {"氧分压记录值0", 26, 0, NULL, NULL, &odata.o2_p_record[0], 2, NULL}, 
    {"氧分压记录值1", 26, 0, NULL, NULL, &odata.o2_p_record[1], 2, NULL},
    {"氧分压记录值2", 26, 0, NULL, NULL, &odata.o2_p_record[2], 2, NULL},
    {"氧分压记录值3", 26, 0, NULL, NULL, &odata.o2_p_record[3], 2, NULL}, 
    {"氧分压记录值4", 26, 0, NULL, NULL, &odata.o2_p_record[4], 2, NULL},
    {"氧分压记录值5", 26, 0, NULL, NULL, &odata.o2_p_record[5], 2, NULL},
    {"本次系统时间", 26, 0, NULL, NULL, &odata.t_sys, 4, NULL},
    {"总系统时间", 26, 0, NULL, NULL, &odata.t_sys_totoal, 4, NULL}, 
    {"本次制氧时间", 26, 0, NULL, NULL, &odata.t_work, 4, NULL}, 
    {"总制氧时间", 26, 0, NULL, NULL, &odata.t_work_totoal, 4, NULL},
    {"主板维护时间统计", 26, 0, NULL, NULL, &odata.twh_main_board, 4, NULL}, 
    {"分子筛维护时间", 26, 0, NULL, NULL, &odata.twh_fenzishai, 4, NULL}, 
    {"空气过滤器", 26, 0, NULL, NULL, &odata.twh_air_filter, 4, NULL},  
    {"氧分压显示值", 26, 0, NULL, NULL, &osta.o2_pres_display, 2, NULL},    
    {"分子筛效率下降故障", 26, 0, NULL, NULL, &bit_list[1].val, 1, NULL},
    {"电磁阀转换故障", 26, 0, NULL, NULL, &bit_list[2].val, 1, NULL},
    {"温度过高故障（备用）", 26, 0, NULL, NULL, &bit_list[3].val, 1, NULL},
    {"氧分压超下限故障", 26, 0, NULL, NULL, &bit_list[4].val, 1, NULL},
    {"氧分压过低故障", 26, 0, NULL, NULL, &bit_list[5].val, 1, NULL},   
    {"氧分压超上限故障", 26, 0, NULL, NULL, &bit_list[6].val, 1, NULL},  
    {"三相输出短路故障", 26, 0, NULL, NULL, &bit_list[7].val, 1, NULL}, 
    {"转速过低故障", 26, 0, NULL, NULL, &bit_list[8].val, 1, NULL},
    {"电机过温故障", 26, 0, NULL, NULL, &bit_list[9].val, 1, NULL},
    {"压缩机过温或风机故障", 26, 0, NULL, NULL, &bit_list[10].val, 1, NULL},
    {"交流电源输入电压过高故障", 26, 0, NULL, NULL, &bit_list[11].val, 1, NULL},
    {"功率模块过温故障", 26, 0, NULL, NULL, &bit_list[12].val, 1, NULL},
    {"无效状态或其他状态故障", 26, 0, NULL, NULL, &bit_list[13].val, 1, NULL},   
    {"控制单元维护", 26, 0, NULL, NULL, &bit_list[14].val, 1, NULL},  
    {"分子筛维护", 26, 0, NULL, NULL, &bit_list[16].val, 1, NULL}, 
    {"过滤器维护", 26, 0, NULL, NULL, &bit_list[17].val, 1, NULL},
    {"压缩机启动控制功能故障", 26, 0, NULL, NULL, &bit_list[18].val, 1, NULL}, 
    {"压缩机停止控制功能故障", 26, 0, NULL, NULL, &bit_list[19].val, 1, NULL},
    {"机上告警功能故障", 26, 0, NULL, NULL, &bit_list[20].val, 1, NULL}, 
    {"控制单元故障", 26, 0, NULL, NULL, &bit_list[21].val, 1, NULL},
    {"温度传感器故障", 26, 0, NULL, NULL, &bit_list[22].val, 1, NULL}, 
    {"氧分压传感器故障", 26, 0, NULL, NULL, &bit_list[23].val, 1, NULL},
    {"oxy_cal_k", 26, 0, NULL, NULL, &ocfg.oxy_cal_k, 4, NULL}, 
    {"oxy_cal_b", 26, 0, NULL, NULL, &ocfg.oxy_cal_b, 2, NULL},
    {"sova1_disable", 26, 0, NULL, NULL, &ocfg.s_manage.sova1_disable , 1, NULL},
    {"sova1_t_sw", 26, 0, NULL, NULL, &ocfg.s_manage.sova1_t_sw , 1, NULL},
    {"sova2_disable", 26, 0, NULL, NULL, &ocfg.s_manage.sova2_disable , 1, NULL},
    {"sova2_t_sw", 26, 0, NULL, NULL, &ocfg.s_manage.sova2_t_sw , 1, NULL},
    {"sova2_t_sque", 26, 0, NULL, NULL, &ocfg.s_manage.sova2_t_sque, 1, NULL}, 
    {"sova_t_warp", 26, 0, NULL, NULL, &ocfg.s_manage.sova_t_warp, 1, NULL},
    {"compr1_err_t", 26, 0, NULL, NULL, &ocfg.s_manage.compr1_err_t , 1, NULL},
    {"compr2_err_t", 26, 0, NULL, NULL, &ocfg.s_manage.compr2_err_t , 1, NULL},
    {"compr3_err_t", 26, 0, NULL, NULL, &ocfg.s_manage.compr3_err_t , 1, NULL},
    {"compr4_err_t", 26, 0, NULL, NULL, &ocfg.s_manage.compr4_err_t , 1, NULL},
    {"compr5_err_t", 26, 0, NULL, NULL, &ocfg.s_manage.compr5_err_t , 1, NULL},    
    {NULL},
};




void sta_page_htm(const char *name, const char *body, void (fun_add_elemnet)(unsigned char))
{
	http_sprintf_init();
	http_sprintf("%s%s", HTML_PARM_HEAD, body);
	http_sprintf(REQUST_JSCRIPT_HEAD, name);
	fun_add_elemnet(URI_REPOS_HTML);
	REQUST_JSCRIPT_END(name);
	http_sprintf_send();
}

static void _add_htm_element(unsigned char mode)
{
	unsigned int sip,dip;
    const unsigned char *lip, *mask, *gw, *otaip;
	unsigned short sport,dport,otaport;
	unsigned char *id;
	if(mode == URI_REPOS_HTML){
		list_htm_jsreq(&list);
	}
	else if(mode == URI_REPOS_JSON){
	    list_htm_jsrepos(&list);
	}
}


void http_html_sta(const char *name, void (fun_add_elemnet)(unsigned char))
{
	http_sprintf_init();
	http_sprintf("%s%s", HTML_PARM_HEAD, C_PAGE_BODY_0);
    list_htm_body(&list);
    http_sprintf("%s", C_PAGE_BODY_2);
    http_sprintf(STA_REFRESH_HTML_START);
	fun_add_elemnet(URI_REPOS_HTML);
	http_sprintf(STA_REFRESH_HTML_END);
	http_sprintf_send();
}

void http_json_sta(void (fun_add_elemnet)(unsigned char))
{
	http_sprintf_init();
	http_sprintf("{");
	fun_add_elemnet(URI_REPOS_JSON);
	http_sprintf("}");
	http_sprintf_send();
}

void parm1_pos_htm(unsigned char mode)
{
	if(mode == URI_REPOS_HTML){
		http_html_sta(HTML_PAGE1_NAME, _add_htm_element);
	}
	else{
		http_json_sta(_add_htm_element);
	}
}




